2 Dimensional Local Histogram Equalization
In [59]:
#insert your own path here
import os
os.chdir('/Users/albertlee/NeuroCV/Module1/PCV/2Dhisteq')
In [22]:
### Run below if necessary
##import sys
##sys.path.append('/usr/local/lib/python2.7/site-packages')
import math
import csv,gc
import matplotlib
import numpy as np
import cv2
#%matplotlib
BINS = 32
In [23]:
import matplotlib.pyplot as plt
%matplotlib inline
In [24]:
from skimage import data, img_as_float
from skimage import exposure
import cv2
In [63]:
from PCV import tools
from PIL import Image
from numpy import *
In [64]:
im = array(Image.open('Fear199.png'))
plt.imshow(im)
Out[64]:
In [75]:
im = cv2.imread('Fear199.png',0)
img = np.asarray(im)
In [76]:
imgflat = img.reshape(-1)
print imgflat.sum()
print " "
fig = plt.hist(imgflat, bins=255)
plt.title('Histogram')
plt.show()
print " "
#clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
clahe = cv2.createCLAHE()
#img_grey = np.array(img * 255, dtype = np.uint8)
#threshed = cv2.adaptiveThreshold(img_grey, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 0)
cl1 = clahe.apply(im)
#cv2.imwrite('clahe_2.jpg',cl1)
#cv2.startWindowThread()
#cv2.namedWindow("adaptive")
#cv2.imshow("adaptive", cl1)
#cv2.imshow("adaptive", threshed)
#plt.imshow(threshed)
print " "
localimgflat = cl1.reshape(-1)
print localimgflat
print localimgflat.sum()
print " "
fig = plt.hist(localimgflat, bins=255)
plt.title('Locally Equalized Histogram')
plt.show()
In [77]:
cv2.imwrite('AdaptiveFear199.jpg',cl1)
plt.imshow(cl1)
Out[77]:
In [78]:
image = array(Image.open('AdaptiveFear199.png'))
plt.imshow(np.asarray(image))
Out[78]:
In [46]:
import scipy.misc
rgb = scipy.misc.toimage(cl1)
plt.imshow(cl1) ## This alternate cl1 was constructed by applying a gray filter over the existing filter
Out[46]:
In [53]:
im = cv2.imread('adaptexample.png',0)
img = np.asarray(im)
In [69]:
imgflat = img.reshape(-1)
print imgflat.sum()
print " "
fig = plt.hist(imgflat, bins=255)
plt.title('Histogram')
plt.show()
print " "
#clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
clahe = cv2.createCLAHE()
#img_grey = np.array(img * 255, dtype = np.uint8)
#threshed = cv2.adaptiveThreshold(img_grey, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 0)
cl1 = clahe.apply(im)
#cv2.imwrite('clahe_2.jpg',cl1)
#cv2.startWindowThread()
#cv2.namedWindow("adaptive")
#cv2.imshow("adaptive", cl1)
#cv2.imshow("adaptive", threshed)
#plt.imshow(threshed)
print " "
localimgflat = cl1.reshape(-1)
print localimgflat
print localimgflat.sum()
print " "
fig = plt.hist(localimgflat, bins=255)
plt.title('Locally Equalized Histogram')
plt.show()
Out[69]:
In [56]:
cv2.imwrite('clahe_2.jpg',cl1)
plt.imshow(cl1)
Out[56]:
In [68]:
image = array(Image.open('adaptimagefinal.png'))
plt.imshow(np.asarray(image))
Out[68]:
In [47]:
gray_image = cv2.cvtColor(cl1, cv2.COLOR_BGR2GRAY)
plt.imshow(gray_image)
In [37]:
from skimage import color
from skimage import io
img = color.rgb2gray(io.imread('Fear199.png'))
plt.imshow(img)
Out[37]:
In [40]:
rgbimg = Image.new("RGBA", img.size)
newimg = rgbimg.paste(img)
plt.imshow(newimg)
In [42]:
grey()
In [ ]: